package com.wjh.anyview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.anyview.dao.InstitutionMapper;
import com.wjh.anyview.entity.dto.CommonResult;
import com.wjh.anyview.entity.po.Institution;
import com.wjh.anyview.service.InstitutionService;
import com.wjh.anyview.service.constant.StringConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author wjh
 * @date 2021/10/9 20:28
 * @Package com.wjh.anyview.service.impl
 */
@Service
@Slf4j
public class InstitutionServiceImpl implements InstitutionService {
    @Resource
    private InstitutionMapper institutionMapper;

    @Override
    @Transactional
    public CommonResult<?> saveInstitution(Institution institution) {
        //查重
        QueryWrapper<Institution> wrapper = new QueryWrapper<>();
        wrapper.eq("school", institution.getSchool());
        wrapper.eq("college", institution.getCollege());

        //重复添加，无效
        if (institutionMapper.selectOne(wrapper) != null) {
            return CommonResult.operateFailWithMessage("该学术机构已经存在本系统！");
        }

        //添加成功
        if (institutionMapper.insert(institution) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(StringConstant.SYSTEM_BUSY);
    }
}
